package com.example.demo.mapper;

import com.example.demo.model.Comment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface CommentMapper {
    @Select("SELECT * FROM comment")
    List<Comment> selectAllComments();

    @Select("SELECT * FROM comment WHERE id = #{id}")
    Comment selectCommentById(Long id);

    @Select("SELECT * FROM comment WHERE username = #{username}")
    List<Comment> selectCommentByUserName(String username);

    @Insert("INSERT INTO comment (parent_id, title, content, user_id, username, create_time, reply_time) " +
            "VALUES (#{parentId}, #{title}, #{content}, #{userId}, #{username}, #{createTime}, #{replyTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertComment(Comment comment);

    @Insert("DELETE FROM comment WHERE id = #{id}")
    void deleteCommentById(Long id);

    @Insert("UPDATE comment SET title = #{title}, content = #{content}, reply_time = #{replyTime} WHERE id = #{id}")
    void updateComment(Comment comment);
}
